﻿using System;
using System.Collections.Generic;
using System.Text;
using XNAchievement;
using XNAchievement.Achievements;

namespace AchievementTestGame
{
    class DisplayAchievementsScreen : MenuScreen
    {
        //This formatter is used for the details screen. It would be a good idea to
        //extend the formatter (or create a new one) to show more information
        private DefaultAchievementFormatter formatter = new DefaultAchievementFormatter();

        public DisplayAchievementsScreen()
            : base("View Achievements")
        {
        }

        public override void LoadContent()
        {
            //We must do this in LoadContent, in the ctor is ScreenManager null.
            foreach (Achievement a in ScreenManager.AchievementManager.GetVisibleAchievements()) //Only visible achievements are shown on this page!
            {
                //Create a Button for each achievement, clicking it will pop up a details screen.
                MenuEntry tmp = new MenuEntry(a.Name);
                tmp.Selected += new EventHandler<PlayerIndexEventArgs>(tmp_Selected);
                MenuEntries.Add(tmp);
            }

            MenuEntry BackMenuEntry = new MenuEntry("Back");
            BackMenuEntry.Selected += OnCancel;
            MenuEntries.Add(BackMenuEntry);

            base.LoadContent();
        }

        void tmp_Selected(object sender, PlayerIndexEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Achievement Information");

            Achievement achievement = null;
            //This is more or less a hack: We iterate all achievements until we find one with the correct name
            //in a better menu system you could attach the achivement (or at least its internal name)
            //to the MenuEntry... But for now we stay with this.
            foreach (Achievement a in ScreenManager.AchievementManager.GetVisibleAchievements())
            {
                if (a.Name == ((MenuEntry)sender).Text)
                {
                    achievement = a;
                    break;
                }
            }

            sb.Append(achievement.AsString(formatter));

            ScreenManager.AddScreen(new MessageBoxScreen(sb.ToString(), false), null);
        }
    }
}
